package cn.jbolt.ai.app.vo;

import cn.hutool.core.util.ClassUtil;
import cn.jbolt.util.JSONUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;

//@JsonIgnoreProperties(ignoreUnknown = true)
public class ActionData {
    //action动作

    public static final String ACTION_PING = "ping";

    /**
     * 正常请求
     */
    public final static String ACTION_REQUEST = "request";
    /**
     * 重新生成
     */
    public final static String ACTION_REGENERATE = "regenerate";
    /**
     * 取消
     */
    public final static String ACTION_CANCEL = "cancel";
    /**
     * 响应
     */
    public final static String ACTION_RESPONSE = "response";
    /**
     * 思考过程
     */
    public final static String ACTION_THINKING = "thinking";
    /**
     * 结束
     */
    public final static String ACTION_OVER = "over";
    /**
     * 异常错误
     */
    public final static String ACTION_ERROR = "error";
    /**
     * 同步后端数据的消息,将后端数据库的数据发送给前端，绑定到前端消息组件上
     */
    public static final String ACTION_SYNCDATA = "syncData";
    //引用
    public final static String ACTION_REFERENCE = "reference";
    public final static String ACTION_UPDATE_SESSION = "update_session";//这个状态是返回给前端的,更新session信息的

    //前端传过来的flag,是一个消息的标识
    private String flag;
    //session表中的seesionId
    private Long sessionId;
    private Long app;
    private Long msgId;
    //前端反过来的动作:request,cancel 返回的的动作:response,over,error
    private String action;
    //数据
    private JsonNode data;
    //全局数据,一个json串,存放全局数据: functionCell的ids;model;systemRole;appId;maxTokens;topP;topK;enableContext
    private JsonNode global;

    /**
     * 从一个已有的socket数据复制一份
     * @param copyData
     * @return
     */
    public static ActionData copy(ActionData copyData) {
        ActionData data = new ActionData();
        data.setFlag(copyData.getFlag());
        data.setApp(copyData.getApp());
        data.setSessionId(copyData.getSessionId());
        data.setGlobal(copyData.getGlobal());
        return data;
    }

    /**
     * 是否是测试模式，socketdata， global中传入 debugMode: true
     * @return
     */
    @JsonIgnore
    public boolean isDebugMode() {
        return global != null && global.has("debugMode") && global.get("debugMode").asBoolean();
    }


    @Override
    public String toString() {
        return JSONUtil.toJson(this);
    }

    public JsonNode getData() {
        return data;
    }

    @JsonIgnore
    public AiChatData  getDataAsChatData() {
        return getDataAs(AiChatData.class);
    }
    public <T> T getDataAs(Class<T> clazz) {
        return JSONUtil.toObject(data, clazz);
    }

    public ActionData setData(Object data) {
        if (data == null) {
            this.data  = null;
        } else if (data instanceof JsonNode) {
            this.data = (JsonNode) data;
        } else if (ClassUtil.isSimpleValueType(data.getClass()) ) {
            this.data = JSONUtil.createObjectNode().putPOJO("value", data);
        } else {
            this.data = JSONUtil.toJsonObject(data);
        }
        return this;
    }

    public String getFlag() {
        return flag;
    }

    public ActionData setFlag(String flag) {
        this.flag = flag;
        return this;
    }

    public Long getSessionId() {
        return sessionId;
    }

    public ActionData setSessionId(Long sessionId) {
        this.sessionId = sessionId;
        return this;
    }

    public Long getApp() {
        return app;
    }

    public ActionData setApp(Long app) {
        this.app = app;
        return this;
    }

    public String getAction() {
        return action;
    }

    public ActionData setAction(String action) {
        this.action = action;
        return this;
    }

    public JsonNode getGlobal() {
        return global;
    }

    public ActionData setGlobal(JsonNode global) {
        this.global = global;
        return this;
    }

    public Long getMsgId() {
        return msgId;
    }

    public void setMsgId(Long msgId) {
        this.msgId = msgId;
    }
}


